package com.leon.raw_consumer;

import com.leon.raw_consumer.tool.RpcConsumer;
import com.leon.raw_consumer.tool.RpcConsumerEx;
import com.leon.rpc.RpcRequest;
import com.leon.service.UserService;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ClientBootstrap {


    public static void main(String[] args) {
       zkTest();
    }

    /**
     * 基于 zookeeper 实现服务发现
     */
    private static void zkTest() {
        try {
            ExecutorService executor = Executors.newFixedThreadPool(5);
            RpcConsumerEx rpcConsumerEx = new RpcConsumerEx();

            RpcRequest request = new RpcRequest();
            request.setClassName("com.leon.service.UserService");
            UserService service = (UserService) rpcConsumerEx.createProxy(UserService.class, request);

            while (true) {
                try {
                    executor.submit(() -> {
                        System.out.println(service.getUserName("invoke rpc service " ));
                    });

                    Thread.sleep(2000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 基本 Netty 使用实现 RPC
     */
    private static void basicUse() {
        try {
            RpcConsumer consumer = new RpcConsumer();

            RpcRequest request = new RpcRequest();
            request.setClassName("com.leon.service.UserService");
            UserService service = (UserService) consumer.createProxy(UserService.class, request);
            while (true) {
                System.out.println(service.getUserName("invoke rpc service"));
                Thread.sleep(2000);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
